package visualization;

import infovis07contest.data.Genre;
import infovis07contest.data.Movie;
import infovis07contest.data.MovieDB;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

import processing.core.PApplet;

public class ZoomWindow {

	private int x;
	
	private int y;
	
	private int w;

	private int g;
	
	private int h;
	
	private int dx = 9;
	
	private int dy = 9;
	
	private int yr;
	
	private int yrIndex;
	
	private int mi = 0;
	

	
	private boolean drawText=false;
	
	public ZoomWindow(int x, int y, int g, int yr, int w, int h ) {
		this.x = x;
		this.y = y;
		this.g = g;
		this.h = h;
		this.w = w;
		this.yr = yr;
		//update();


		
	}
	
	public void draw(PApplet p) {
		
		

		int xi = 0;
		int yi = 0;
		
		p.fill(0,128,227);

		p.stroke(0,110,205);
		p.strokeWeight(1);
		int tempx = this.x + (w/2) - ((dx* Launcher.rowlength[g][yrIndex])/2);

		if (drawText) {
			


			int tempy = 0;
			if (Launcher.rowlength[g][yrIndex] != 0) {
				 tempy = this.y + (dy * 8) - ((dy * (Launcher.movieCount[g][yrIndex]/Launcher.rowlength [g][yrIndex]))/2);
				 if (tempy < 0) {
					 tempy = 0;
				 }
			} 
			for (int i = 0; i < Launcher.sorted[g][yrIndex].length; i++) {
				p.fill(Launcher.cs[Launcher.ci[g][yrIndex][i]][0],Launcher.cs[Launcher.ci[g][yrIndex][i]][1], Launcher.cs[Launcher.ci[g][yrIndex][i]][2]);
				//p.fill(Launcher.rc[g][yrIndex][i],Launcher.gc[g][yrIndex][i],Launcher.bc[g][yrIndex][i]);
				p.rect(tempx+xi,tempy+yi,dx,dx);
				xi += dx;
				if ( xi/dx >= Launcher.rowlength[g][yrIndex]) {  //xi >=w-dx) {
					xi = 0;
					yi += dy;
				}
				
			}
			
			int ytext = tempy+yi+90;
			int ymark = ytext;
		

			try {
			
				Movie m = Launcher.sorted[g][yrIndex][mi];
				p.image(Launcher.top,x+100,0,115,20);
				p.image(Launcher.left,tempx-30,20,20,115);
			
				//draw background of box
				//p.fill(204,204,204);
				//p.noStroke();
				//p.rect(x +10, 260, w-40, 200);
				
				//draw the text
				p.stroke(0);
				p.fill(0);
				p.textFont(Launcher.f16);
				p.text(m.title, x+20, ymark);
				p.textFont(Launcher.d14);
				ymark += 20;
				p.text("Director: ", x+20, ymark );
				ymark += 15;
				for (int i = 0 ; i < m.directors.size(); i++) {
					p.text(m.directors.get(i).name, x+40, ymark);
					ymark += 15;
				}

				p.text("Actors: ", x+20, ymark );
				ymark += 15;
				for (int i = 0 ; i < m.actors.size() && i < 20; i++) {
					p.text(m.actors.get(i).name, x+40, ymark);
					ymark += 15;
				}
				ymark = ytext+20;
				
				p.text("IMDB: " + m.imdbRating, w/2 + x+20, ymark);
				p.fill(240);
				if (m.imdbRating>=0)
					p.rect(w/2 + x +20 + p.textWidth("IMDB:            "), ymark-10, 80, 10);
				int alpha = m.imdbNumRatings;
				if (alpha > 255) alpha = 255;
				if (m.imdbRating<4) p.fill(240,10,10,alpha);
				else if (m.imdbRating>=7) p.fill(0,224,35,alpha);
				else p.fill(255,212,0,alpha);
				if (m.imdbRating>=0){
				
					p.rect(w/2 + x +20 + p.textWidth("IMDB:            "), ymark-10, 80*(m.imdbRating/10), 10);

				}
				ymark += 20;
				p.fill(0);
				
				String snr = Float.toString(m.netflixRating).substring(0, 3);
				p.text("Netflix: " + snr, w/2 +  x+20, ymark);
				alpha = m.netflixNumRatings;
				if (alpha > 255) alpha = 255;
				p.fill(240);
				if (m.netflixRating>=0)
				p.rect(w/2 + x +20 + p.textWidth("IMDB:            "), ymark-10, 80, 10);
				p.fill(0,224,35);
				if (m.netflixRating<2) p.fill(240,10,10,alpha);
				else if (m.netflixRating>=3.5) p.fill(0,224,35,alpha);
				else p.fill(255,212,0,alpha);
				if (m.netflixRating>=0)
				p.rect(w/2 + x +20 + p.textWidth("IMDB:            "), ymark-10, 80*(m.netflixRating/5), 10);
				ymark += 20;
				p.fill(0);
				
				
				
				p.text("Cinematographers: ", w/2 + x + 20, ymark );
				ymark += 15; 
				for (int i = 0 ; i < m.cinematographers.size(); i++) {
					p.text(m.cinematographers.get(i).name, w/2 + x + 40, ymark);
					ymark += 15;
				}
				

			}catch(ArrayIndexOutOfBoundsException e)	{
			//	System.err.println("out of bounds");
			}

			
			
		}
		//p.noStroke();
		

		p.stroke(0);
		p.strokeWeight(1.5f);
	}
	

	
	public void findBoxUnderMouse(int mx, int my) {
		
		int tempy = 0;
		if (Launcher.rowlength[g][yrIndex] != 0) {
			 tempy = (dy * 8) - ((dy * (Launcher.movieCount[g][yrIndex]/Launcher.rowlength [g][yrIndex]))/2);
			 if (tempy < 0) {
				 tempy = 0;
			 }
		} 
		
		int yh = ((Launcher.sorted[g][yrIndex].length/Launcher.rowlength[g][yrIndex])*dy) ;
		if ((Launcher.sorted[g][yrIndex].length%Launcher.rowlength[g][yrIndex])!=0) {
			yh+= dy;
		}
		
		
		if (( 0<= (my - tempy) ) && ((my - tempy) < (yh+dy))) {
		
			int yindex = (my - tempy) / dy;
	
	
			int tempx = (w/2) - ((dx* Launcher.rowlength[g][yrIndex])/2);
			
			int xw = dx*Launcher.rowlength[g][yrIndex] ;

			if ((0 <= (mx - tempx)) && ((mx - tempx) <= xw)) {
				int xindex = (mx - tempx) /dx;
		
				int numInRow = Launcher.rowlength[g][yrIndex];
				int index = xindex + (yindex*numInRow);
			
				if((index < Launcher.sorted[g][yrIndex].length) && (0 <= index  ) ) {
					mi = index;
				}
			}

		}
	}
	
	public void clickUnderMouse(int mx, int my) {
		
		int tempy = 0;
		if (Launcher.rowlength[g][yrIndex] != 0) {
			 tempy = (dy * 8) - ((dy * (Launcher.movieCount[g][yrIndex]/Launcher.rowlength [g][yrIndex]))/2);
			 if (tempy < 0) {
				 tempy = 0;
			 }
		} 
		
		int yh = ((Launcher.sorted[g][yrIndex].length/Launcher.rowlength[g][yrIndex])*dy) ;
		if ((Launcher.sorted[g][yrIndex].length%Launcher.rowlength[g][yrIndex])!=0) {
			yh+= dy;
		}
		
		
		if (( 0<= (my - tempy) ) && ((my - tempy) < (yh+dy))) {
		
			int yindex = (my - tempy) / dy;
	
	
			int tempx = (w/2) - ((dx* Launcher.rowlength[g][yrIndex])/2);
			
			int xw = dx*Launcher.rowlength[g][yrIndex] ;

			if ((0 <= (mx - tempx)) && ((mx - tempx) <= xw)) {
				int xindex = (mx - tempx) /dx;
		
				int numInRow = Launcher.rowlength[g][yrIndex];
				int index = xindex + (yindex*numInRow);
			
				if((index < Launcher.sorted[g][yrIndex].length) && (0 <= index  ) ) {
					 Launcher.ci[g][yrIndex][index]++;
					 Launcher.ci[g][yrIndex][index] =  Launcher.ci[g][yrIndex][index] %4;

				}
			}

		}
	}

	/**
	 * @return the x
	 */
	public int getX() {
		return x;
	}

	/**
	 * @param x the x to set
	 */
	public void setX(int x) {
		this.x = x;
	}

	/**
	 * @return the y
	 */
	public int getY() {
		return y;
	}

	/**
	 * @param y the y to set
	 */
	public void setY(int y) {
		this.y = y;
	}

	/**
	 * @return the w
	 */
	public int getW() {
		return w;
	}

	/**
	 * @param w the w to set
	 */
	public void setW(int w) {
		this.w = w;
	}

	/**
	 * @return the h
	 */
	public int getH() {
		return h;
	}

	/**
	 * @param h the h to set
	 */
	public void setH(int h) {
		this.h = h;
	}

	/**
	 * @return the currentG
	 */
	public int getG() {
		return g;
	}

	/**
	 * @param currentG the currentG to set
	 */
	public void setG(int g) {
		this.g = g;
	}

	/**
	 * @return the currentYr
	 */
	public int getYr() {
		return yr;
	}

	/**
	 * @param currentYr the currentYr to set
	 */
	public void setYr(int yr) {
		this.yr = yr;
	}

	/**
	 * @return the dx
	 */
	public int getDx() {
		return dx;
	}

	/**
	 * @param dx the dx to set
	 */
	public void setDx(int dx) {
		this.dx = dx;
	}

	/**
	 * @return the yrIndex
	 */
	public int getYrIndex() {
		return yrIndex;
	}

	/**
	 * @param yrIndex the yrIndex to set
	 */
	public void setYrIndex(int yrIndex) {
		this.yrIndex = yrIndex;
	}

	/**
	 * @return the drawText
	 */
	public boolean isDrawText() {
		return drawText;
	}

	/**
	 * @param drawText the drawText to set
	 */
	public void setDrawText(boolean drawText) {
		this.drawText = drawText;
	}
	
	
}
